//
//  CircleViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/18.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//**************标签**************
#import "CircleViewController.h"
#import "TagModel.h"
@interface CircleViewController ()<UITableViewDataSource,UITableViewDelegate>
/*
 *数据源
 */
@property(nonatomic,copy)NSMutableArray *dataArray;

@property(nonatomic,copy)UITableView *myTable;
@end

@implementation CircleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:[UIView new]];
    self.view.backgroundColor=[UIColor whiteColor];
    [self initData];
    [self createNavigation];
    [self createUI];
    [self getAllBiaoQian];

    // Do any additional setup after loading the view.
}
#pragma mark 初始化数据
-(void)initData
{
    _dataArray = [NSMutableArray array];
//    NSArray *arr=@[@"名医坐诊",@"红人馆",@"步步夺金",@"中医养生",@"美食营养",@"减肥瘦身",@"运动健身",@"母婴育儿",@"情感婚姻",@"女性健康",@"美容养颜",@"健康商城"];
//    _dataArray=[NSMutableArray arrayWithArray:arr];
}
-(void)createNavigation
{
    self.navigationController.navigationBar.hidden=NO;
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"标签";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    if (_biaoqianState==1) {
        self.navigationController.navigationBar.hidden=YES;
        self.tabBarController.tabBar.hidden=NO;
    }else{
        
    }
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createUI
{
    UITableView *table=[[UITableView alloc]initWithFrame:CGRectMake(0, 34, self.view.frame.size.width, self.view.frame.size.height-34) style:UITableViewStyleGrouped];
    table.showsVerticalScrollIndicator=NO;
    table.delegate=self;
    table.dataSource=self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    _myTable=table;
    
}
//tableView的代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    TagModel *model=_dataArray[indexPath.row];
    
    //标签
    UILabel *biaoqian=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.view.frame.size.width-10-10, 49.0)];
    biaoqian.text=model.name;
    biaoqian.textColor=[UIColor blackColor];
    biaoqian.font=[UIFont systemFontOfSize:15];
    [cell.contentView addSubview:biaoqian];
    //分割线
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 49.5, self.view.frame.size.width, 0.5)];
    line.backgroundColor=[UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
    [cell.contentView addSubview:line];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TagModel *model=_dataArray[indexPath.row];
    
    if (_biaoqianState==0) {
        self.returnCircle(model.name,model.tagId);
        [self.navigationController popViewControllerAnimated:YES];
    }else if (_biaoqianState==1){
        TieZiViewController *vc=[[TieZiViewController alloc]init];
        vc.biaoti=model.name;
        vc.tagId=model.tagId;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

//获取所有系统标签
-(void)getAllBiaoQian
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    [self.view isLoading:YES];
    
    [manager GET:[NSString stringWithFormat:AllBiaoQian,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
         [self.view isLoading:NO];
        NSArray *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"所有系统标签===============%@",dic);
        _dataArray=[NSMutableArray array];
        if (dic) {
            for (NSDictionary *dic1 in dic) {
                TagModel *model=[[TagModel alloc]init];
                model.tagId=dic1[@"id"];
                model.name=dic1[@"name"];
                model.is_offical=dic1[@"is_offical"];
                model.created_at=dic1[@"created_at"];
                [_dataArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [_myTable reloadData];
            });
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         [self.view isLoading:NO];
        NSLog(@"%@",error.localizedDescription);
    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
